<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Gio_Core_Application
{
	private $_view = null;
	private $_plugins = array();
	
	private static $_route;
	
	private static $_instance = null;
	
	/**
	 * @return Gio_Core_Application
	 */
	public static function getInstance()
	{
		if (null == self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	/**
	 * @return Gio_Core_Application
	 */
	public function __construct()
	{
		/**
		 * Run Plugin Localization 
		 */
		Gio_Core_Plugins_LocalizationRoute::getInstance()->run();
		$this->_view = Gio_Core_View::getInstance();
		return $this;
	}
	
	/**
	 * @return Gio_Core_Application
	 */
	public function registerPlugin($className, $params = null)
	{
		$plugin = array(
					'name' => $className,
					'params' => $params,
				);
		$this->_plugins[] = $plugin;
		return $this;
	}
	
	/**
	 * @return Gio_Core_Application
	 */
	public function removePlugins($name = null)
	{
		if (null == $name) {
			$this->_plugins = array();
			return $this;
		}
		unset($this->_plugins[$name]);
		return $this;
	}
	
	/**
	 * @param Gio_Core_Page
	 */
	private function _runPlugin()
	{
		if (!empty($this->_plugins)) {
			foreach ($this->_plugins as $index => $plugin) {
				$object = new $plugin['name']();
				call_user_func(array($object, 'run'));
			}
		}
	}
	
	public static function _initInstallChecker()
	{
		$configs = Gio_Core_Config_Xml::getConfig('install');
		//$route = Gio_Core_Route::getCurrentRoute();
		if (null == $configs || !isset($configs->date) 
					|| null == (string)$configs->date || (string)$configs->date == '') 
		{
			return false;
		}
		return true;
	}
	
	public static function getRoute($index = null)
	{
		if ($index && isset(self::$_route[$index])) {
			return self::$_route[$index];
		}
		return self::$_route;
	}
	
	public function run()
	{
		/**
		 * Run Permalink Plugin 
		 */
		$permalink = new Modules_Core_Controllers_Plugins_Permalink();
		call_user_func(array($permalink, 'run'));
		
		/**
		 * Run LocalizationRoute Plugin 
		 */
		$localizationRoute = new Gio_Core_Plugins_LocalizationRoute();
		call_user_func(array($localizationRoute, 'run'));
		
		/**
		 * Run Install Checker
		 */
		$currentRoute = Gio_Core_Route::getCurrentRoute();
		self::$_route = $currentRoute;
		
		$controllerFront = Gio_Core_Controller::getIntance();
		
		if ($currentRoute && $currentRoute['name'] != 'core_install_step1' 
										&& self::_initInstallChecker() === false) 
		{
			$controllerFront->redirect($this->_view->url('core_install_step1'));
			return;
		}
		
		if (isset($currentRoute['module']) && isset($currentRoute['controller'])
				&& isset($currentRoute['action']))
		{
			if (self::_initInstallChecker() == true) {
				$csrf = new Gio_Core_Csrf($currentRoute['name']);
				if (!$csrf->authentication()) {
					return;
				}
				$csrf->appendElement();
			}
			
			$controllerFront->setModuleName($currentRoute['module'])
							->setControllerName($currentRoute['controller'])
							->setActionName($currentRoute['action']);
			/**
			 * Run Plugins First
			 */
			if (self::_initInstallChecker() == true) {
				$this->_runPlugin();
			}
		
			$this->_view->CURRENT_ROUTE = $currentRoute['name'];
			$content = $controllerFront->dispatch();
			$this->_view->setLayoutContent($content);
			if (!Gio_Core_Controller::getIntance()->getDisableLayout()) {
				$layout = (isset($this->_view->APP_LAYOUT) && $this->_view->APP_LAYOUT)
									? $this->_view->APP_LAYOUT : $controllerFront->getLayout();
				$layoutFile = TEMPLATE_DIR . DS . $this->_view->APP_TEMPLATE . DS . 'layouts' . DS . $layout . '.phtml';
				$this->_view->fetch($layoutFile);
			} else {
				$this->_view->content($content);
			}
		}
	}
}